<?php

namespace App\Admin\Controllers;

use AlibabaCloud\Tea\Request;
use App\Admin\Repositories\GrType;
use App\Models\Admin;
use App\Models\Type;
use App\service\ReturnService;
use App\service\ReviewImages;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class GrTypeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GrType(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('type_name')->tree();
            $grid->column('type_img')->image('', 100, 100);
            $grid->column('type_status')->switch();
            $grid->column('sort');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('type_name');
                $filter->equal('type_status');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GrType(), function (Show $show) {
            $show->field('id');
            $show->field('type_name');
            $show->field('type_img');
            $show->field('type_status');
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GrType(), function (Form $form) {
            $form->display('id');
            $form->text('type_name')->required();
            $form->select('pid')->options('getType')->load('twoType', 'twoType');

            $form->select('twoType');

            $form->image('type_img')->uniqueName()->saveFullUrl()->autoUpload();
            $form->switch('type_status')
                ->customFormat(function ($v) {
                    return $v == '打开' ? 1 : 0;
                })
                ->saving(function ($v) {
                    return $v ? '1' : '0';
                });

            $form->text('sort')->required();
            $form->saving(function (Form $form) {
                // 判断是否是新增操作
                if ($form->isCreating()) {
                    $img=$form->type_img;
                    if (!empty($img)){
                        $reviewImage=new ReviewImages();
                        $res=$reviewImage->verify($img);
                        if($res!="合规"){
                            return  $form->response()->error('图片不合规');
                        }
                    }
                    $pid = $form->pid;
                    $twoType = $form->twoType;
                    //判断获取pid 为null 时，默认为一级
                    if ($pid == null) {
                        $form->pid = 0;
                        $form->deleteInput('twoType');
                    }
                    //当一级不为空，二级为空时
                    if ($pid != null && $twoType == null) {
                        $form->pid = $pid;
                        $form->deleteInput('twoType');
                    }
                    //当一级不为空，二级不为空时
                    if ($pid != null && $twoType != null) {
                        $form->pid = $twoType;
                        $form->deleteInput('twoType');
                    }
                } else {
                    $pid = $form->pid;
                    $twoType = $form->twoType;
                    //判断获取pid 为null 时，默认为一级
                    if ($pid == null) {
                        $form->pid = 0;
                        $form->deleteInput('twoType');
                    }
                    //当一级不为空，二级为空时
                    if ($pid != null && $twoType == null) {
                        $form->pid = $pid;
                        $form->deleteInput('twoType');
                    }
                    //当一级不为空，二级不为空时
                    if ($pid != null && $twoType != null) {
                        $form->pid = $twoType;
                        $form->deleteInput('twoType');
                    }
                }

            });


            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    /**
     * @param int $id
     * 删除
     */
    public function destroy($id)
    {
        $data = Type::where('id', $id)->first()->toArray();
        $typeData = Type::where('pid', $id)->get()->toArray();
        if ($data['type_status'] != 1 && empty($typeData)) {
            return $this->form()->destroy($id);
        }

    }

    /**
     * 商品分类
     */
    public function goodsType()
    {
        //$typeData=  Type::leftJoin('gr_goods','gr_type.pid','=','gr_goods.goods_type_id')->get();
        $typeData = \App\Models\GrType::all()->toArray();
        $data = self::getTree($typeData);
        if (!empty($data)) {
            return ReturnService::successReturn('200', '成功', $data);
        } else {
            return ReturnService::failedReturn('400', '失败');
        }
    }

    /**
     * 递归
     */
    public static function getTree($data, $pid = 0)
    {
        $tress = [];
        foreach ($data as $v) {
            if ($v['pid'] == $pid) {
                $v['son'] = self::getTree($data, $v['id']);
                $tress[] = $v;
            }
        }
        return $tress;
    }


}
